/**
 * 
 */
package com.gr.staffpm.widget.calendar.event;

import java.util.List;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.tasks.service.TaskService;


/**
 * @author Graham Rhodes 7 Jan 2011 09:36:18
 */
public class EventPanel extends Panel {

    private static final long serialVersionUID = 1L;
    @SpringBean(name = "taskService")
    private TaskService taskService;
    private java.util.Calendar cal = java.util.Calendar.getInstance();
    private final String cssClass;
    private final RepeatingView eventPanel;

    public EventPanel(String id, java.util.Calendar calendar, String cssClass) {
        super(id);
        this.cssClass = cssClass;

        this.cal.setTime(calendar.getTime());
        eventPanel = new RepeatingView("eventPanel");

        renderTasks();

        add(eventPanel);
    }

    public EventPanel(String id, java.util.Calendar calendar) {
        this(id, calendar, "");
    }

    /* (non-Javadoc)
     * @see org.apache.wicket.markup.html.panel.Panel#onComponentTag(org.apache.wicket.markup.ComponentTag)
     */
    @Override
    protected void onComponentTag(ComponentTag tag) {
        super.onComponentTag(tag);
        tag.getAttributes().put("class", cssClass);
    }

    public void renderTasks() {
        eventPanel.removeAll();
        //TODO Only bring back tasks assigned to me if I'm a member
        //TODO If I'm a manager bring back tasks assigned to me or tasks in my owned projects
        List<Task> tasks = taskService.getTasksDueOn(cal.getTime());
        //TODO Check if there are more tasks due on this day then we can render. And act accordingly.
        for (Task task : tasks) {
            eventPanel.addOrReplace(new TaskEvent(String.valueOf(task.getTaskId()), task));
        }
    }

    public java.util.Calendar getCal() {
        return cal;
    }

    public void setCal(java.util.Calendar cal) {
        this.cal = cal;
    }

}
